/**
 * This file is part of CommaTuner.
 * Copyright 2011 Gabriel G Mendonça (gabrielgmendonca@gmail.com)
 * 
 * CommaTuner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * CommaTuner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CommaTuner.  If not, see <http://www.gnu.org/licenses/>.
 */

package br.mendonca.commatuner;

import android.os.SystemClock;
import android.util.Log;
import br.mendonca.audio.analysis.Pitch;
import br.mendonca.audio.analysis.PitchDetector;

/**
 * A Thread that does the pitch detection and updates the interface.
 * 
 * @author gmendonca
 */
public class PitchDetectionThread extends Thread {

	/** Indicates whether this Thread should continue running. */
	public boolean active;

	private PitchDetector pitchDetector;
	private PitchView pitchView;

	public PitchDetectionThread(PitchDetector pitchDetector, PitchView pitchView) {
		this.pitchDetector = pitchDetector;
		this.pitchView = pitchView;
	}

	@Override
	public void run() {
		while (active) {
			long startTime = SystemClock.uptimeMillis();

			Pitch pitch = pitchDetector.detect();
			pitchView.update(pitch);

			long now = SystemClock.uptimeMillis();
			Log.d("CommaTuner", "Elapsed Time: " + (now - startTime));
		}
	}
}
